package com.easy;

/**
 * @author Dianzhi Han
 * @version 1.0
 * @description: TODO
 * @date 2022/10/21 19:54
 */
public class Num190_reverseBits {
    // you need treat n as an unsigned value
    public int reverseBits(int n){
        int high = 31;
        int length = getlength(n);
        int result = 0;
        for(int i = length;i >0; i--){
            int lower = n & 1;
            int temp = lower << (high--);
            result = result | temp;
            n = n >>> 1;
        }
        return result;
    }


    public int getlength(int n){
        int sum = 0;
        while(n!=0){
            n = n >>> 1;
            sum++;
        }
        return sum;
    }
}
